package de.toad.MCEController;

import net.rim.device.api.system.*;
import net.rim.device.api.collection.util.*;

class DataStore {
   
   private static PersistentObject store;
   private static DataStore instance;
   private LongHashtableCollection  settings;
   
   private static final long KEY_HOSTNAME = 0;

   private static String DEFAULT_HOSTNAME = "192.168.42.10:40511";
   
   private static DataStore getInstance() {
        if (null == instance) {
            instance = new DataStore();
        }
        return instance; 
    }
    
    private DataStore() {
        store = PersistentStore.getPersistentObject(0x3f7526a461d097dcL);
    }
    
    public static String getHostname() {
       String sHostname = (String)getInstance().get(KEY_HOSTNAME);
       if (null == sHostname || sHostname.length() == 0) {
         sHostname =  DEFAULT_HOSTNAME; 
         setHostname(sHostname);
       }
       return sHostname;
    }
   
    public static void setHostname(String sHostname) {
       getInstance().set(KEY_HOSTNAME, sHostname);
    } 
    
    private void set(long key, Object value) {
        synchronized(store) {
            settings = (LongHashtableCollection)store.getContents();
            if (null == settings) {
                settings = new LongHashtableCollection();
            }
            settings.put(key,value);   
            store.setContents(settings);
            store.commit();
        }
    }    
    
    private Object get(long key) {
        synchronized(store) {
            settings = (LongHashtableCollection)store.getContents();
            if (null != settings && settings.size() != 0) {
                 return settings.get(key);
            } else {
                 return null;
            }
        }
    }
   
} 

